package com.etoak.common.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class RedisService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public void set(String key, String value, long expire, TimeUnit timeUnit) {
        stringRedisTemplate.opsForValue().set(key, value, expire, timeUnit);
    }

    /**
     * String 的get命令
     *
     * @param key
     * @return
     */
    public String get(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public void hset(String key, String filed, String value) {
        stringRedisTemplate.opsForHash().put(key, filed, value);

    }

    public List<String> hvals(String key) {
        return stringRedisTemplate.opsForHash().values(key).stream().map(Object::toString).toList();
    }

    public void hdel(String key, String filed) {
        stringRedisTemplate.opsForHash().delete(key, filed);
    }

    public void del(String key) {
        stringRedisTemplate.delete(key);
    }
}
